package com.example.service.shopping;


import com.example.common.enums.RoleEnum;
import com.example.mapper.appointment.UserMapper;
import com.example.mapper.medical.DoctorMapper;
import com.example.mapper.shopping.UserCommentMapper;
import com.example.model.entity.appointment.User;
import com.example.model.entity.medical.Doctor;
import com.example.model.entity.other.Account;
import com.example.model.entity.shopping.UserComment;
import com.example.model.vo.UserCommentVO;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 药品规格表业务处理
 **/
@Service
public class UserCommentService {

    @Resource
    private UserCommentMapper userCommentMapper;
    @Resource
    private DoctorMapper doctorMapper;
    @Resource
    private UserMapper userMapper;

    /**
     * 新增
     */
    public void add(UserComment userComment) throws ParseException {
        String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        userComment.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(format));
        userCommentMapper.insert(userComment);
    }

    /**
     * 删除
     */
    public void deleteById(Integer id) {
        userCommentMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            userCommentMapper.deleteById(id);
        }
    }

    /**
     * 修改
     */
    public void updateById(UserComment userComment) {
        userCommentMapper.updateById(userComment);
    }

    /**
     * 根据ID查询
     */
    public UserComment selectById(Integer id) {
        return userCommentMapper.selectById(id);
    }

    /**
     * 查询所有
     */
    public List<UserCommentVO> selectAll(UserComment userComment) {
        UserCommentVO userCommentVO=new UserCommentVO();
        BeanUtils.copyProperties(userComment,userCommentVO);
        Account currentUser = TokenUtils.getCurrentUser();
        if(RoleEnum.BUSINESS.name().equals(currentUser.getRole())){
            userCommentVO.setBusinessId(currentUser.getId());
        }
        return userCommentMapper.selectAll(userCommentVO);
    }

    /**
     * 分页查询
     */
    public PageInfo<UserCommentVO> selectPage(UserComment userComment, Integer pageNum, Integer pageSize) {
        UserCommentVO userCommentVO=new UserCommentVO();
        BeanUtils.copyProperties(userComment,userCommentVO);
        Account currentUser = TokenUtils.getCurrentUser();
        if(RoleEnum.BUSINESS.name().equals(currentUser.getRole())){
            userCommentVO.setBusinessId(currentUser.getId());
        }
        PageHelper.startPage(pageNum, pageSize);
        List<UserCommentVO> list = userCommentMapper.selectAll(userCommentVO);
        return PageInfo.of(list);
    }


    public List<UserCommentVO> selectByMedicinesId(Integer medicinesId) {
        List<UserComment> userCommentList = userCommentMapper.selectMedicinesId(medicinesId);
        List<UserCommentVO> userCommentVOList = new ArrayList<>();
        for (UserComment userComment : userCommentList) {
            List<Doctor> doctorList = new ArrayList<>();
            List<User> userList = new ArrayList<>();
            UserCommentVO userCommentVO = new UserCommentVO();
            BeanUtils.copyProperties(userComment, userCommentVO); // 确保正确复制字段
            if (userComment.getDoctorId() != null) {
                Doctor doctor = doctorMapper.selectById(userComment.getDoctorId());
                doctorList.add(doctor);
            }
            if (userComment.getUserId() != null) {
                User user = userMapper.selectById(userComment.getUserId());
                userList.add(user);
            }
            userCommentVO.setDoctorList(doctorList);
            userCommentVO.setUserList(userList);
            userCommentVOList.add(userCommentVO);
        }

        return userCommentVOList;
    }


}
